<?php

/**
 * Engine One iCal generator
 * Please read the LICENSE file
 *
 * @copyright oneDot only <we@onedotonly.be>
 * @author Koen Betsens <koen@onedotonly.be>
 * @package Engine
 * @license GNU Lesser General Public License
 */

 
// includes
require_once ( "engine/www/combustion.php");
require_once ( "engine/ext/ical/iCalcreator.class.php" );


// globals
global $DB, $CALENDAR, $Object;


// default variables
$CAL = new CALENDAR();
$now = date('Y-m-d', time());

// check get variables
switch ($_GET['action']) {
	
	// single calendar event
	case 'single':
		
		// create or get file 
		$filename	= "ical.single." . (int) $_GET['ID'] . ".ics";
		$file_name 	= ROOT . ACCOUNT . "/www/DB/media/ical.single." . (int) $_GET['ID'] . ".ics";
		
		$read		= false;
		if( file_exists( $file_name ) && filectime( $file_name ) < strtotime( '-10 days' ))
			$read	= true;

		// check on update or create new file
		if( !$read) {
		
			$nodes	= $CAL->filter_nodes_by_object( "cal.single", (int) $_GET['type'], (int) $_GET['ID']);
			$title = $nodes[0]->obj->get('name');
			$title = strtolower(str_replace(' ', '_', $title));
			$v = ical_render($nodes, $title);

			// save calendar to file and return
			$v->setConfig( "directory", ROOT . ACCOUNT . "/www/DB/media/" ); 
			$v->setConfig( "filename", $filename );
			$v->saveCalendar();
			$v->returnCalendar(); 
				
		} else {
		
			// create new calendar by parsing an old file
			$calendar = new vcalendar();
			$calendar->setConfig( 'url', $file_name );
			$calendar->parse();
			$calendar->returnCalendar(); 			
			
		}
			
		break;
		
	// all calendar events for 1 day
	case 'day':
		
		// create or get file 
		$filename	= "ical.day." . (int) $_GET['day'] . ".ics";
		$file_name = ROOT . ACCOUNT . "/www/DB/media/ical.day." . (int) $_GET['day'] . ".ics";
		
		// day variables
		$day	= $_GET['day'];
		$day 	= substr($day,0,4) . '-' . substr($day,4,2) . '-' . substr($day,6,2) . ' ' . substr($day,8,2) . ':' . substr($day,10,2) . ':' . substr($day,12,2);
		
		$read		= false;
		if( file_exists( $file_name ) && filectime( $file_name ) < strtotime( '-10 days' ))
			$read	= true;

		// check on update or create new file
		if( !$read) {
			
			// new ics file
			$nodes 	= $CAL->filter_nodes( substr($day,0,10), 1, "cal.single", false, false, 24 );
			$title 	= $day;
			$v = ical_render($nodes, $title);

			// save calendar to file and return
			$v->setConfig( "directory", ROOT . ACCOUNT . "/www/DB/media/" ); 
			$v->setConfig( "filename", $filename );
			$v->saveCalendar();
			$v->returnCalendar(); 
		
		} else {
		
			// create new calendar by parsing an old file
			$calendar = new vcalendar();
			$calendar->setConfig( 'url', $file_name );
			$calendar->parse();
			$calendar->returnCalendar(); 			
		}

		break;
	
	
	// all calendar events for 1 type
	case 'type':
		
		// create or get file 
		$filename	= "ical.type." . $DB->simplestring( $_GET['type'] ) . ".ics";
		$file_name  = ROOT . ACCOUNT . "/www/DB/media/ical.type." . $DB->simplestring( $_GET['type'] ) . ".ics";
		
		$read		= false;
		if( file_exists( $file_name ) && filectime( $file_name ) < strtotime( '-10 days' ))
			$read	= true;

		if( !$read ) {
	
			// new ics file
			$nodes 	= $CAL->filter_nodes( "now()", 31, "cal.single", $DB->simplestring( $_GET['type'] ), false, 24 );
			$v = ical_render( $nodes, $DB->simplestring( $_GET['type'] ));

			// save calendar to file and return
			$v->setConfig( "directory", ROOT . ACCOUNT . "/www/DB/media/" ); 
			$v->setConfig( "filename", $filename );
			$v->saveCalendar();
			$v->returnCalendar(); 
		
		} else {
		
			$calendar = new vcalendar();
			$calendar->setConfig( 'url', $file_name );
			$calendar->parse();
			$calendar->returnCalendar(); 			
		}

		break;
		
	// all calendar events
	default:
		
		// create or get file 
		$filename	= "ical.calendar.ics";
		$file_name 	= ROOT . ACCOUNT . "/www/DB/media/ical.calendar.ics";
		
		$read		= false;
		if( file_exists( $file_name ) && filectime( $file_name ) < strtotime( '-10 days' ))
			$read	= true;

		// check on update or create new file
		if( !$read) {
		
			$nodes = $CAL->filter_nodes( 'now()', 365, "cal.single", false, false, 150 );	
			$title = 'calendar';				

			// save calendar to file and return
			$v = ical_render( $nodes, 'calendar' );
			$v->setConfig( "directory", ROOT . ACCOUNT . "/www/DB/media/" ); 
			$v->setConfig( "filename", $filename );
			$v->saveCalendar();
			$v->returnCalendar(); 
			
		
		} else {
	
			// create new calendar by parsing an old file
			$calendar = new vcalendar();
			$calendar->setConfig( 'url', $file_name );
			$calendar->parse();
			$calendar->returnCalendar(); 
			
		}

		break;
		
}

# function to render events in ical output
function ical_render($nodes, $title) {
	global $DB;
    	
	// setup new calendar	
	$v = new vcalendar();
	
	$v->setProperty('X-WR-CALNAME', engine_get('title.control') . ' ' . $title);
	$v->setProperty('X-WR-CALDESC', 'Event listing - supplied by ' . engine_get('url'));
	$v->setProperty('X-WR-TIMEZONE', 'Europe/Brussels' ); 		
	$v->setProperty( "calscale", "GREGORIAN" ); 
	//$v->setConfig( 'filename',  $title . '.' . engine_get('title.control') . '.ics' );
	
	// add vevents 
	foreach($nodes as $event) {
	
		if ( $event->obj->values['name'] ) {
		
			//print_r($event);		
			$e = new vevent();
			
			// properties of the event
			// start date
			$dtstart = strtotime($event->get('date'));			
			$e->setProperty('dtstart', 
				date('Y', $dtstart),
				date('m', $dtstart),
				date('d', $dtstart),
				date('H', $dtstart),
				date('i', $dtstart),
				date('s', $dtstart));
			
			// duration
			//if ($event->get('ctype') != 0) {	
			//	$e->setProperty('duration', 0, $event->get('span')); # Set duration in days
			//} else {
				$e->setProperty('duration', 0, 0, 0, $event->get('span')); # Set duration in hours
			//}
			
			// summary & description
			$summary 	= htmlspecialchars($event->obj->get('name'));
			$summary 	= str_replace ('<br>', '\n', $summary );
			$summary 	= str_replace ('&amp;', '&', $summary );
			$e->setProperty('summary', $summary);
			
			$descstring = $DB->query_direct( "SELECT txtdump.long FROM txtdump WHERE textid = " . $event->obj->get('textid') . " 
				AND lang = '" . engine_get( 'lang.master' ) . "'");
			$descstring = stripslashes( $descstring );
			$descstring = str_replace ('<br>', '\n', $descstring );
			$descstring = str_replace ('&amp;', '&', $descstring );
			$descstring = html_entity_decode( strip_tags ($descstring ) );
			// $descstring = strip_tags( htmlentities ( $descstring ) );		
			$e->setProperty('description', $descstring);
			
			// location
			$e->setProperty('location', htmlspecialchars($event->loc->get('name')));
						
			// add event to calendar
			$v->addComponent( $e );	
			
		}	
	
	}
	return $v;
}

?>
